<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>v-for</title>
    <script src="../assets/js/vue.js"></script>
</head>
<body>
    <h1>v-for</h1>
    <hr>
    <div id="app">
       <ul>
           <li v-for="item in sortItems">{{item}}</li>
       </ul>
       <hr>
       <ul>
           <li v-for="student,i in sortStudents">
               {{i}}--{{student.name}}--{{student.age}}
           </li>
       </ul>
    </div>
</body>
<script>
    var app = new Vue({
        el:"#app",
        data:{ 
            items:[20,23,18,65,32,19,5,56,41],
            students:[
                {name:'jspang',age:32},
                {name:'Panda',age:30},
                {name:'PanPaN',age:21},
                {name:'King',age:45}
            ]
        },
        computed:{
            sortItems:function(){
                return this.items.sort(this.sortNumber)
            },
            sortStudents:function(){
                // return sortByKey(this.students,'age')
                return this.sortByKey(this.students,'age')
            }
        },
        methods:{
            sortNumber:function(a,b){
                return a-b;
            },
            sortByKey:function(array,key){
                return array.sort(function(a,b){
                    var x=a[key];
                    var y=b[key];
                    return ((x<y)?-1:((x>y)?1:0));
                });
            }
        }
    });
    //数组对象方法排序:
        // function sortByKey(array,key){
        //     return array.sort(function(a,b){
        //         var x=a[key];
        //         var y=b[key];
        //         return ((x<y)?-1:((x>y)?1:0));
        //     });
        // }
</script>
</html>